package com.flyco.modules.stm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectList;
import com.flyco.modules.oms.entity.OmsOrder;
import com.flyco.modules.oms.entity.OmsOrderItem;
import com.flyco.modules.oms.service.IOmsOrderItemService;
import com.flyco.modules.oms.service.IOmsOrderService;
import com.flyco.modules.pms.entity.PmsProduct;
import com.flyco.modules.pms.service.IPmsProductService;
import com.flyco.modules.stm.entity.StmStoreInfo;
import com.flyco.modules.stm.entity.StmStoreProductStock;
import com.flyco.modules.stm.mapper.StmStoreProductStockMapper;
import com.flyco.modules.stm.service.IStmStoreInfoService;
import com.flyco.modules.stm.service.IStmStoreProductStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;

/**
 * @Description: 门店商品库存表
 * @Author: flyco
 * @Date: 2024-01-03
 * @Version: V1.0
 */
@Service
public class StmStoreProductStockServiceImpl extends ServiceImpl<StmStoreProductStockMapper, StmStoreProductStock> implements IStmStoreProductStockService {


    @Autowired
    private IStmStoreInfoService storeInfoService;
    @Autowired
    private IOmsOrderService orderService;
    @Autowired
    private IOmsOrderItemService orderItemService;
    @Autowired
    private StmStoreProductStockMapper productStockMapper;
    @Autowired
    private IPmsProductService productService;


    @Override
    public void init() {

        //查询所有有效门店
        List<StmStoreInfo> storeList = storeInfoService.getStoreList();


        //依次查询门店所有订单
        for (StmStoreInfo storeInfo : storeList) {

            List<OmsOrder> orderList = orderService.getOrderList(storeInfo.getStoreId());

            //根据订单到订单明细表查询商品购买数量
            for (OmsOrder order : orderList) {

                List<OmsOrderItem> orderItemList = orderItemService.selectByMainId(order.getId());

                for (OmsOrderItem item : orderItemList) {
                    updateStock(storeInfo.getStoreId(), item.getProductId(), item.getProductQuantity());
                }
            }
        }
    }

    public void updateStock(Long storeId, Long productId, Integer productQty) {
        //判断是否存在，存在更新，不存在，则插入
        StmStoreProductStock productStock = getByStoreAndProductId(storeId, productId);
        if (productStock != null) {
            productStock.setProductQty(productStock.getProductQty() + productQty);
            productStockMapper.updateById(productStock);
        } else if (productQty > 0) {
            //productQty>0 表示是新增购买量，如果productQty<0 则是退货，没有找到数据，则不操作。
            addProductStock(storeId, productId, productQty);
        }
    }

    /**
     * 查询门店商品的库存信息
     */
    public StmStoreProductStock getByStoreAndProductId(Long storeId, Long productId) {

        LambdaQueryWrapper<StmStoreProductStock> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StmStoreProductStock::getStoreId, storeId)
                .eq(StmStoreProductStock::getProductId, productId);

        StmStoreProductStock productStock = productStockMapper.selectOne(wrapper);

        return productStock;

    }

    public void addProductStock(Long storeId, Long productId, Integer productQty) {

        PmsProduct productInfo = productService.getById(productId);
        if (productInfo != null) {
            StmStoreProductStock stock = new StmStoreProductStock();
            stock.setProductId(productId);
            stock.setStoreId(storeId);
            stock.setProductQty(productQty);
            stock.setCreateTime(new Date());
            stock.setProductName(productInfo.getName());
            stock.setProductSpec(productInfo.getSkuSpec() + productInfo.getUnit() + "/" + productInfo.getSkuUnit());

            productStockMapper.insert(stock);
        }
    }

}
